package com.eatme.web.sys.service.impl;

import com.eatme.web.common.vo.AppResponse;
import com.eatme.web.sys.model.*;
import com.eatme.web.sys.repository.SysRoleRepository;
import com.eatme.web.sys.repository.SysUserRepository;
import com.eatme.web.sys.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("sysUserServiceImpl")
public class SysUserServiceImpl implements SysUserService {

    private static final Logger LOG = LoggerFactory.getLogger(SysUserServiceImpl.class);
    @Autowired
    SysRoleServiceImpl sysRoleServiceImpl;

    @Autowired
    PasswordEncoder passwordEncoder;
    

    @Autowired
    SysUserRepository sysUserRepository;

    @Autowired
    SysRoleRepository sysRoleRepository;

    @Override
    public List<SysUser> getAll() {
        return null;
    }

    @Override
    public Permission findPermission(String userId) {
        Permission permission = new Permission();
        //permission.setMenu(sysResourceDao.findMenu(userId));
        //List<String> method = sysResourceDao.getMethod(userId);
        //permission.setMethod(method);
        return permission;
    }

    @Override
    public AppResponse add(com.eatme.web.sys.entity.SysUser sysUser) {
        sysUser = sysUserRepository.save(sysUser);
        return AppResponse.ok().data(sysUser);
    }

    @Override
    public AppResponse batchImport(List<com.eatme.web.sys.entity.SysUser> list) {
        sysUserRepository.saveAll(list);
        return AppResponse.ok().message("操作成功!");
    }

    @Override
    public AppResponse export() {
        List<com.eatme.web.sys.entity.SysUser> list = sysUserRepository.findAllByStatus(1);
        return AppResponse.ok().data(list).message("操作成功!");
    }

    @Override
    public boolean isUsernameExisted(String username) {
        return sysUserRepository.existsByUsername(username);
    }

    @Override
    public boolean isEmailExisted(String email) {
        return sysUserRepository.existsByMail(email);
    }

    @Override
    public boolean isTelephoneExisted(String telephone) {
        return sysUserRepository.existsByTelephone(telephone);
    }

    /**
     * findMenu 查菜单
     *
     *
     *         SELECT c.`id`,c.`name`,c.`parent_id`,c.`icon`,c.`component`,c.`path`,c.`redirect`,c.`title`,a.`user_id` FROM sys_role_user a
     *                             LEFT JOIN sys_role_resource b ON a.`role_id` =  b.`role_id`
     *                             LEFT JOIN sys_role d ON b.`role_id` =d.`id`
     *                             LEFT JOIN sys_resource c ON b.`resource_id` =c.`id`
     *         WHERE a.`user_id` = #{userId} AND d.`status` =1 AND c.`id` IS NOT NULL and c.`type` != 2 GROUP BY c.`id`
     *
     * getMethod 查权限
     *
     *         SELECT c.`name` FROM sys_role_user a
     *                             LEFT JOIN sys_role_resource b ON a.`role_id` =  b.`role_id`
     *                             LEFT JOIN sys_resource c ON b.`resource_id` =c.`id`
     *                             LEFT JOIN sys_role d ON b.`role_id` =d.`id`
     *         WHERE a.`user_id` = #{userId} AND d.`status` =1 AND c.`id` IS NOT NULL AND c.name LIKE '%.%' GROUP BY c.`id`
     *
     * @param name
     * @return
     */
    @Override
    public com.eatme.web.sys.entity.SysUser getUserInfo(String name) {
        com.eatme.web.sys.entity.SysUser sysUser = sysUserRepository.findSysUserByUsername(name);
        Set<com.eatme.web.sys.entity.SysResource> sysResourceSet = new HashSet<>();
        sysUser.getSysRoles().forEach(item-> {
            item.getSysResources().forEach(i->{
                sysResourceSet.add(i);
            });
        });
        Permission permission = new Permission();
        List<com.eatme.web.sys.entity.SysResource> menu = new ArrayList<>();
        List<String> method = new ArrayList<>();
        sysResourceSet.forEach(item->{
            if (item.getName().contains(".")) {
                method.add(item.getName());
            }else {
                menu.add(item);
            }
        });
        permission.setMenu(menu);
        permission.setMethod(method);
        sysUser.setPermission(permission);
        return sysUser;
    }
}
